package com.pioneer.controller.stock;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.constant.StockConstants;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.stock.domain.StoInout;
import com.pioneer.web.stock.service.IStoInoutService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 销售出库单
 *
 * @author hlm
 */
@RestController
@RequestMapping("/stock/out")
public class StoOutController extends BaseController {

    @Resource
    private IStoInoutService inoutService;

    /**
     * 获取销售出库单列表
     *
     * @param inout 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:out:list')")
    @GetMapping("/list")
    public AjaxResult list(StoInout inout) {
        boolean isPage = startPage();
        inout.setType(StockConstants.INOUT_TYPE_OUT);
        List<StoInout> list = inoutService.list(Wrappers.query(inout));
        return success(isPage, list);
    }

    /**
     * 根据销售出库单id获取详细信息
     *
     * @param inoutId 销售出库单id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:out:query')")
    @GetMapping(value = "/{inoutId}")
    public AjaxResult getInfo(@PathVariable Long inoutId) {
        return AjaxResult.success(inoutService.selectOutById(inoutId));
    }

    /**
     * 修改销售出库单
     *
     * @param inout 销售出库单
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:out:edit')")
    @Log(title = "销售出库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody StoInout inout) {
        return toAjax(inoutService.updateOut(inout));
    }

    /**
     * 确认出库
     * @param inout 销售出库单
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:out:out')")
    @Log(title = "销售出库单", businessType = BusinessType.OTHER)
    @PostMapping("/out")
    public AjaxResult inoutOut(@RequestBody StoInout inout){
        return toAjax(inoutService.inoutOut(inout));
    }
}
